#!/usr/bin/env python
import os,sys,re,csv
import numpy as np
from networkx import Graph
from SpectralMix import DistanceMatrix
from SpectralMix import SpectralCluster
from SpectralMix.DataManipLib import make_input_graph
from examples_data import data1, data2
from matplotlib.patches import Circle
from matplotlib.collections import PatchCollection

sys.path.append(os.path.join("..","tests","data"))
from GenesExample import geneList, geneLabels, geneDistDict

G = make_input_graph(geneDistDict,geneList)                                                                                                        
k = 2                                                                                                                                              
sigma = 0.14
print "\n" 


print len(geneList), len(geneLabels), len(G.nodes())

## run the spectral clustering on original data                                                                                                    
sc = SpectralCluster(G,k=k,dataHeader=geneList,labels=geneLabels,projID=os.path.join(".","temp"),                                                  
                     dtype='graph',weighted=True,verbose=True,sigma=sigma,penalty=True)     

print sc.evaluation

sc = SpectralCluster(G,k=k,dataHeader=geneList,labels=geneLabels,projID=os.path.join(".","temp"),                                                  
                     dtype='graph',weighted=True,verbose=True,sigma=sigma,penalty=True,refine='kmeans')     

print sc.evaluation
print sc.inferenceEvaluation
print sc.inferenceResults['labels']


#sc = SpectralCluster(G,k=k,dataHeader=geneList,labels=geneLabels,projID=os.path.join(".","temp"),                                                  
#                     dtype='graph',weighted=True,verbose=True,sigma=sigma,penalty=True,classifyStep='profdpm')
#
#print sc.evaluation



'''
data = data1
geneList = np.array(data['geneList'])
pathways = data['pathways']
labels = np.array(data['labels'])
edges = data['edges']


print 'geneList',len(geneList)
print 'num pathways',len(pathways)
print 'labels', len(labels)

### error checking
if len(geneList) != len(list(set(geneList))):
    "ERROR: gene and labels do not match"
    sys.exit()

### create a graph from the data
G = Graph()
newLabels = [] 
for edge,distance in edges.iteritems():  
    geneI,geneJ = edge.split('#')
    if geneI not in geneList:
        print "WARNING: gene in edges that is not in geneList",geneI
        continue
    if geneJ not in geneList:
        print "WARNING: gene in edges that is not in geneList",geneJ
        continue

    #print geneI, geneJ, distance
    if distance != None:
        G.add_edge(geneI,geneJ,weight=distance)

## spectral clustering variables
sigma= 0.1
k = 2

sc = SpectralCluster(G,k=k,dataHeader=geneList,labels=labels,projID=os.path.join(".","figures",'GeneSetExample'),
                     dtype='graph',weighted=True,verbose=True,sigma=sigma) 

print '\taMat',np.shape(sc.aMat)
print '\tyMat',np.shape(sc.yMat)

print 'recall',sc.evaluation['recall'],'precision',sc.evaluation['precision'] 
print 'sigma', sc.sigHat


k = 2
sc = SpectralCluster(G,k=k,dataHeader=geneList,labels=labels,projID=os.path.join(".","figures",'GeneSetExample'),
                     dtype='graph',weighted=True,verbose=True,sigma=sigma,penalty=True) 

print '\taMat',np.shape(sc.aMat)
print '\tyMat',np.shape(sc.yMat)

print 'recall',sc.evaluation['recall'],'precision',sc.evaluation['precision'] 
print 'sigma', sc.sigHat
'''
